1941D - Rudolf and the Ball Game - CodeForces Solution


dp implementation

Please click on ads to support us..

Python Code:

t = int(input())
while t:
    n, m, st = input().split()
    n = int(n)
    m = int(m)
    sol = {}
    sol[int(st)-1] = True
    final_sol = {}
    for i in range(0, int(m)):
        l1, sign = input().split()
        l1 = int(l1)
        temp_list = []
        if sign == "?":
            for k, v in sol.items():
                temp_list.append((k+l1)%n)
                temp_list.append((k+n-l1)%n)
        elif sign == "0":
            for k, v in sol.items():
                temp_list.append((k+l1)%n)
        elif sign == "1":
            for k, v in sol.items():
                temp_list.append((k+n-l1)%n)
        sol = {}
        for each in temp_list:
            if each not in sol:
                sol[each] = True
        if i == int(m) -1:
            for each in temp_list:
                if each not in final_sol:
                    final_sol[each] = True
                            
            
    final_l = []
    for k, v in final_sol.items():
        final_l.append(k)
    final_l.sort() 
    print(len(final_l))
    for each in final_l:
        print(each+1, end=" ")
    print("")
    t -= 1


Comments

Submit
0 Comments
More Questions

1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats